﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using VMuktiAPI;

namespace VMuktiGrid.CustomMenu
{
    /// <summary>
    /// Interaction logic for addBuddy.xaml
    /// </summary>
    public partial class ctlPgTabHeader : UserControl
    {
        #region Public Properties
        private string strTitle;

        public string Title
        {
            set
            {
                strTitle = value;
                try
                {
                    for (int i = 0; i < ((Grid)this.Content).Children.Count; i++)
                    {
                        if (((Grid)this.Content).Children[i].GetType() == typeof(Border))
                        {
                            if (((Border)((Grid)this.Content).Children[i]).Child.GetType() == typeof(TextBlock))
                            {
                                ((TextBlock)((Border)((Grid)this.Content).Children[i]).Child).Text = value;
                            }
                            else if (((Border)((Grid)this.Content).Children[i]).Child.GetType() == typeof(TextBox))
                            {
                                ((TextBox)((Border)((Grid)this.Content).Children[i]).Child).Text = value;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    ex.Data.Add("My Key", "Title--:--ctlPgTabHeader.xaml.cs--:--" + ex.Message + " :--:--");
                    ClsException.LogError(ex);
                    ClsException.WriteToErrorLogFile(ex);
                }
                //txtTitle.Text = value;
            }
            get
            {
                return strTitle;
            }
        }

        #endregion

        public ctlPgTabHeader()
        {
            try
            {
                InitializeComponent();
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "ctlPgTabHeader()--:--ctlPgTabHeader.xaml.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
            }
            //txtTitle.MouseDown += new System.Windows.Input.MouseButtonEventHandler(txtTitle_MouseDown);
        }
       
        #region Private Members

        public void txtTitle_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            try
            {
                int count = 0;
                int i = 0;
                //((Border)(((TextBlock)sender).Parent)).Visibility = Visibility.Collapsed;

                for (i = 0; i < ((Grid)this.Content).Children.Count; i++)
                {
                    if (((Grid)this.Content).Children[i].GetType() == typeof(Border))
                    {
                        if (count == 1)
                        {
                            ((Border)((Grid)this.Content).Children[i]).Visibility = Visibility.Visible;
                            ((TextBox)((Border)((Grid)this.Content).Children[i]).Child).Text = strTitle;
                            ((TextBox)((Border)((Grid)this.Content).Children[i]).Child).SelectAll();
                            ((TextBox)((Border)((Grid)this.Content).Children[i]).Child).Focus();
                            e.Handled = true;
                        }
                        else
                        {
                            ((Border)((Grid)this.Content).Children[i]).Visibility = Visibility.Collapsed;
                            count++;
                        }

                    }
                }
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "txtTitle_MouseDown()--:--ctlPgTabHeader.xaml.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
            }
        }

        #endregion

        private void TextBox_LostFocus(object sender, RoutedEventArgs e)
        {
            try
            {
                int count = 0;
                int i = 0;
                //((Border)(((TextBlock)sender).Parent)).Visibility = Visibility.Collapsed;

                for (i = 0; i < ((Grid)this.Content).Children.Count; i++)
                {
                    if (((Grid)this.Content).Children[i].GetType() == typeof(Border))
                    {
                        if (count == 1)
                        {
                            ((Border)((Grid)this.Content).Children[i]).Visibility = Visibility.Collapsed;
                        }
                        else
                        {
                            ((Border)((Grid)this.Content).Children[i]).Visibility = Visibility.Visible;
                            ((TextBlock)((Border)((Grid)this.Content).Children[i]).Child).Text = ((TextBox)sender).Text;
                            strTitle = ((TextBox)sender).Text;
                            count++;
                        }

                    }
                }
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "TextBox_LostFocus()--:--ctlPgTabHeader.xaml.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
            }
        }

        private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Key == Key.Enter)
                {
                    int count = 0;
                    int i = 0;
                    //((Border)(((TextBlock)sender).Parent)).Visibility = Visibility.Collapsed;

                    for (i = 0; i < ((Grid)this.Content).Children.Count; i++)
                    {
                        if (((Grid)this.Content).Children[i].GetType() == typeof(Border))
                        {
                            if (count == 1)
                            {
                                ((Border)((Grid)this.Content).Children[i]).Visibility = Visibility.Collapsed;
                            }
                            else
                            {
                                ((Border)((Grid)this.Content).Children[i]).Visibility = Visibility.Visible;
                                ((TextBlock)((Border)((Grid)this.Content).Children[i]).Child).Text = ((TextBox)sender).Text;
                                strTitle = ((TextBox)sender).Text;
                                count++;
                            }

                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "TextBox_PreviewKeyDown()--:--ctlPgTabHeader.xaml.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
            }
        }
    }
}
